home *** CD-ROM | disk | FTP | other *** search
- Path: magnus.acs.ohio-state.edu!csn!carbon!ouray!exli
- From: exli@ouray.cudenver.edu (ELLIE XIAO-YU LI)
- Newsgroups: comp.lang.c
- Subject: HELP NEEDED: What the hell is wrong with my program?
- Date: 11 Jan 1996 01:55:37 GMT
- Organization: University of Colorado at Denver
- Message-ID: <4d1qmp$h43@carbon.cudenver.edu>
- NNTP-Posting-Host: ouray.cudenver.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
-
- first of all, i have to thank all of you who have answered me through email
- or have followed up my post. and here is more details about my problem.
-
- the system i am using is a dec osf/1. the code is something like:
-
- #include <string.h>
-
- typedef struct {
- .
- .
- .
- char *name;
- .
- .
- .
- } aStruct;
-
- aStruct *ptr;
- char *str1="this is a test";
- char *str2="this is another test";
-
- ptr=(aStruct *)malloc(sizeof(aStruct));
- ptr->name=(char *)malloc(strlen(str1));
- strcpy(ptr->name, str1); /*no problem here*/
- ...
- /*then later i want ptr->name to point to another string*/
- free(ptr->name);
- ptr->name=(char *)malloc(strlen(str2));
- strcpy(ptr->name, str2); /*problem arised here*/
-
-
- i have a struct which one of its fields is a char *. i want to allocate
- memory for it dynamically becuase i don't know the size of the string it
- will point to. so i cannot use an array of char. the first time i
- allocateed memory for it and copied the string into the newly allocated memory
- was perfectly ok. but later in the program i wanted to change the string it
- points to. so i freed up the memory and reallocated another memory block
- and copied the new string into it. then i got thousands of "unalign access"
- messages runing on my screen and the program hangs. i don't know what i did
- wrong here. i tried using the -Wcast-align option (i am using gcc, btw) to
- compile my program but the compiler didn't give me any warning about that.
-
- many of you who replied to me saud that it may be some kind of memory
- address problem as some of the machines require certain data types be stored
- in an even address. so i think it's the system's problem and not my
- program's cause i used the malloc function to allocate memory for my
- pointers. if it's not allowed to put the data in an odd address, then why
- the malloc function would do that? it should be properly aligned by the
- system before this function returns a pointer to a variable. so i still can't
- figure out what the problem is.
-
- so what i did is freed up the entire struct and do the assignments again and
- it finally runs smoothly.
-
- free(ptr);
- ptr=(aStruct *)malloc(sizeof(aStruct));
- ... /*redo all the assigments here*/
- ptr->name=(char *)malloc(strlen(str2));
- strcpy(ptr->name, str2); /*finally get it to change to a new value*/
-
- i don't know if this is the right solution for it. any more insight about
- this? i tried to free up ptr->name and allocate memory for it again after
- it pointed to str2 as a test, and it failed as the first time. i am
- thinking about making the ptr->name NULL after the free function call but i
- haven't tried it yet.
-
-
-
- Mitch
- exli@ouray.cudenver.edu
-